import 'dart:math';

import 'package:erp_widget/erp_widget.dart';
import 'package:flutter/material.dart';

/// 圆形图文悬浮按钮
class DxCircleTextFab extends StatefulWidget {
  /// 是否显示
  final bool isShow;

  /// 是否禁用
  final bool disabled;

  final String? heroTag;

  /// 大小
  final double size;

  /// 边距
  final EdgeInsets margin;

  /// 文本
  final String title;

  /// 优先显示图片
  final String? image;

  /// 如果没有图片就显示图标，没传图标就显示默认的
  final IconData? iconName;

  /// 渐变色
  final Gradient? gradient;

  /// 点击事件
  final VoidCallback? onClick;

  const DxCircleTextFab({
    Key? key,
    this.isShow = true,
    this.disabled = false,
    this.heroTag = 'edit',
    this.size = 60,
    this.margin = const EdgeInsets.only(bottom: 30.0),
    this.title = '添加',
    this.image,
    this.iconName = Icons.add,
    this.gradient = DxStyle.$GRADIENT$4A92E3$4078F4,
    this.onClick,
  }) : super(key: key);

  @override
  State<DxCircleTextFab> createState() => _DxCircleTextFabState();
}

class _DxCircleTextFabState extends State<DxCircleTextFab> with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 300),
      vsync: this,
    )..addListener(() {
        // 结束后重置状态
        if (_controller.status == AnimationStatus.completed) {
          _controller.reset();
        }
      });
  }

  @override
  Widget build(BuildContext context) {
    if (!widget.isShow) return const SizedBox.shrink();
    return Container(
      width: widget.isShow ? widget.size : 0,
      height: widget.isShow ? widget.size : 0,
      margin: widget.margin,
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        gradient: widget.gradient,
        border: Border.all(width: 3, color: Colors.white),
      ),
      foregroundDecoration: widget.disabled
          ? const BoxDecoration(
              shape: BoxShape.circle,
              color: DxStyle.$D8D8D8,
              backgroundBlendMode: BlendMode.lighten,
            )
          : null,
      child: FloatingActionButton(
        isExtended: true,
        heroTag: widget.heroTag,
        elevation: 0,
        splashColor: Colors.transparent,
        focusColor: Colors.transparent,
        backgroundColor: Colors.transparent,
        focusElevation: 0,
        highlightElevation: 0,
        onPressed: () {
          if (widget.disabled) return DxTools.noVip(context);
          widget.onClick?.call();
          if (_controller.isAnimating) return;
          _controller.forward();
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: [
            AnimatedBuilder(
              animation: _controller,
              builder: (_, Widget? child) {
                return Transform.rotate(
                  angle: _controller.value * 2 * pi,
                  child: widget.image != null
                      ? Image.asset(widget.image!, width: 26, height: 26, fit: BoxFit.fill)
                      : Icon(widget.iconName, size: 26,color: Colors.white),
                );
              },
            ),
            Text(widget.title, style: DxStyle.$WHITE$10)
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}
